<?php
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\BaseController;
use Think\Page;
class LeaveController extends BaseController{
    public function add(){       
        $relation=D('Station')
        ->relation(true)
        ->select();
        $this->assign('relationlist',$relation);
        $this->assign('monthlist',D('Month')->select());
        $nowmonth=date('m');
        $this->assign('nowmonth', $nowmonth);        
        $this->assign('title','请假管理');
        $this->assign('info','添加');
        $this->display('Leave_info');
    }
    

    
    public function insert(){
             
        $map['id']=I('post.leaveuser');     
        $staffresult=D('staff')->where($map)->find();
        $data['leaveuser']=$staffresult['name'];
        $data['leaveinfo']=I('post.leaveinfo');
        $data['leavetime']=I('post.leavetime');
        $data['leavedate']=I('post.leavedate');
        $data['mid']=I('mid');
        
        $turename['staff_name']=$staffresult['name'];
        $turename['month']=I('mid');
        $ture=D('Ture')->where($turename)->find();
        $da['taskleave']=I('post.leavedate')+$ture['taskleave'];
        $da['month']=I('mid');
        $savedate['taskleave']=I('post.leavedate');
        $savedate['staff_name']=$staffresult['name'];
        $savedate['month']=I('mid');
        if ($ture>0){
            D('Ture')->where($turename)->save($da);
        }else {
            D('Ture')->add($savedate);
        }
     
        $staff['leave']=$staffresult['leave']+I('post.leavedate');                     
        if (D('Leave')->add($data)){
            if (D('staff')->where($map)->save($staff)){
                $data['state']=1;
                $data['msg']='提交成功';
                $this->ajaxReturn($data);
            }else {
                $data['state']=0;
                $data['msg']='提交失败';
                $this->ajaxReturn($data);
            }          
        }else {
                $data['state']=0;
                $data['msg']='提交失败';
                $this->ajaxReturn($data);
            }         
    }
    
    public function View(){     
      
      
        $a = $_GET['tex'];
        if ($a == null) {
            $a = date('m');
            if ($a < 10) {
                $a = substr($a, 1);
            }
        } else {
            $a = $_GET['tex'];
        }
        
        $search = '%' . htmlspecialchars(I('get.search')) . '%';
        $where['leaveuser'] = array(
            'like',
            $search
        );
        if ($search !== null) {
            $where['leaveuser'] = array(
                'like',
                $search
            );
        }
        
       
        
        $where['mid'] = $a;
        $ss = D('Leave');
        $count = $ss->where($where)->count();
        $page = new Page($count, 20);
        $page->setConfig('first', '首页');
        $page->setConfig('prev', '上一页');
        $page->setConfig('next', '下一页');
        $page->setConfig('last', '末页');
        $page->rollPage = '6';
        $page->setConfig('theme', '%HEADER%  共    %TOTAL_PAGE% 页   %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');
        $show = $page->show();
        
        $result=D('Leave')
        ->where($where)
        ->limit($page->firstRow, $page->listRows)
        ->select();
        
        $this->assign('monthlist', D('Month')->select());
        $this->assign('page', $show);
        $this->assign('null', null);
        $this->assign('month', $a);        
        $this->assign('year', date('Y'));
        $this->assign('title','请假列表页面');
        $this->assign('info','添加');
        $this->assign('leavelist',$result);
        $this->display('Leave_list');
    }
    
    public function edit(){
        $relation=D('Station')
        ->relation(true)
        ->select();
        $this->assign('relationlist',$relation);
        $this->assign('monthlist',D('Month')->select());
        $this->assign('info','修改');
        $this->assign('title','请假编辑页面');
        $where['id']=I('get.id');
        $this->assign('editlist',D('Leave')->where($where)->find());
        $this->display('Leave_info');
    }
    
    public function update(){      
       $where['id']=I('post.id');
      
       $ldate['ldate']=I('post.ldate');
       
       $post['leavedate']=I('post.leavedate');
       $post['leavetime']=I('post.leavetime');
       $post['leaveinfo']=I('post.leaveinfo');
       $post['mid']=I('post.mid');
           
       $leave=D('Leave')->where($where)->find();
       
       $map['month']=I('post.mid');
       $map['staff_name']=$leave['leaveuser'];
             
       $tname=D('Ture')->where($map)->find();
              
       if(!$tname){
           $this->error('相应月份里面没有该员工！');
       }else {       
       $data['taskleave']=$tname['taskleave']+$post['leavedate']-$ldate['ldate'];
       $data['month']=I('post.mid');
       
       if (!D('Leave')->where($where)->find()){
           $this->error('不能修改员工人员！');
       }else {     
           if(false !==D('Leave')->where($where)->save($post)|| 0 !== D('Leave')->where($where)->save($post)){
               if (false !== D('Ture')->where($map)->save($data) ||0 !==D('Ture')->where($map)->save($data)){
                   $this->success('修改成功！',U('Admin/Leave/View'));
               }else {
                   $this->error('修改失败！');
               }
           
           }else {
               $this->error('修改失败！');
           }
       }
     }
   }
    
    public function qjmonth()
    {
        if ($_GET['tex'] > 0) {
            $data['state'] = 1;
            $data['d'] = $_GET['tex'];
            $this->ajaxReturn($data);
        } else {
            $data['state'] = 0;
            $data['msg'] = '请求超时，请重试！';
            $this->ajaxReturn($data);
        }
    }
    
    public function searchqj()
    {
        if ($_GET['tex'] > 0) {
            $data['state'] = 1;
            $data['id'] = $_GET['tex'];
            $data['search'] = $_GET['search'];
            $this->ajaxReturn($data);
        } else {
            $data['state'] = 0;
            $data['msg'] = '请求超时，请重试！';
            $this->ajaxReturn($data);
        }
    }
    
    public function qjgl(){
        $qjid = $_POST['select'];
        $map['id'] = array('in', $qjid );
        if(D('Leave')->where($map)->delete()){
            $data['state']=1;
            $data['msg']='批量删除成功';
            $this->ajaxReturn($data);
        }else {
            $data['state']=0;
            $data['msg']='提交失败';
            $this->ajaxReturn($data);
        }
    }
    
}